﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using ScrumTable.Common;
using ScrumTable.Common.Collections;

#endregion

namespace ScrumTable.DL.Data.TFS.Services.TeamFoundation.Utils
{
    /// <summary>
    /// Contains additional methods for the ProjectCollection class.
    /// </summary>
    public static class ProjectCollectionUtil
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns all projects of a user which has the sufficient right to access them.
        /// </summary>
        /// <param name="projectCollection">Specifies the this ptr of the ProjectCollection.</param>
        /// <param name="forEach">Specifies the action which is executed for each element.</param>
        /// <param name="toCheck">Specifies the predicate to check with each loop.</param>
        /// <returns>Returns the unique id of the team foundation identity.</returns>
        public static void ForEachProjectWithAuthority(this ProjectCollection projectCollection, Action<Project> forEach, Predicate<Project> toCheck)
        {
            PreCondition.AssertNotNull(projectCollection, "projectCollection");
            PreCondition.AssertNotNull(forEach, "forEach");
            PreCondition.AssertNotNull(toCheck, "toCheck");

            GetProjectsWithAuthority(projectCollection, toCheck).ForEach(forEach);
        }

        /// <summary>
        /// Returns all projects of a user which has the sufficient right to access them.
        /// </summary>
        /// <param name="projectCollection">Specifies the this ptr of the ProjectCollection.</param>
        /// <param name="forEach">Specifies the action which is executed for each element.</param>
        /// <returns>Returns the unique id of the team foundation identity.</returns>
        public static void ForEachProjectWithAuthority(this ProjectCollection projectCollection, Action<Project> forEach)
        {
            PreCondition.AssertNotNull(projectCollection, "projectCollection");
            PreCondition.AssertNotNull(forEach, "forEach");

            GetProjectsWithAuthority(projectCollection, p => true).ForEach(forEach);
        }

        /// <summary>
        /// Returns all projects of a user which has the sufficient right to access them.
        /// </summary>
        /// <param name="projectCollection">Specifies the this ptr of the ProjectCollection.</param>
        /// <param name="toCheck">Specifies the predicate to check with each loop.</param>
        /// <returns>Returns the unique id of the team foundation identity.</returns>
        public static IEnumerable<Project> GetProjectsWithAuthority(this ProjectCollection projectCollection, Predicate<Project> toCheck)
        {
            PreCondition.AssertNotNull(projectCollection, "projectCollection");
            PreCondition.AssertNotNull(toCheck, "toCheck");

            foreach (Project project in projectCollection)
            {
                if (project.HasWorkItemReadRights
                    && project.HasWorkItemReadRightsRecursive
                    && project.HasWorkItemWriteRights
                    && project.HasWorkItemWriteRightsRecursive
                    && toCheck(project))
                {
                    yield return project;
                }
            }
        }

        /// <summary>
        /// Returns all projects of a user which has the sufficient right to access them.
        /// </summary>
        /// <param name="projectCollection">Specifies the this ptr of the ProjectCollection.</param>
        /// <returns>Returns the unique id of the team foundation identity.</returns>
        public static IEnumerable<Project> GetProjectsWithAuthority(this ProjectCollection projectCollection)
        {
            return GetProjectsWithAuthority(projectCollection, p => true);
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}